package com.lingo.dingrobotutils.utils;

import com.dingtalk.api.DingTalkSignatureUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @file: com.lingo.dingrobotutils.utils.DingMsgSignUtils
 * @Description: 消息签名算法
 * @author: lingo
 * @version: v1.0
 * @date: 2020/3/24 9:32
 * @Company: www.xyb2b.com
 */

public class DingMsgSignUtils {

    private static Logger logger = LoggerFactory.getLogger(DingMsgSignUtils.class);

    public static String getTimestampSign(long timestamp,String secret){
        String stringToSign = timestamp + "\n" + secret;
        try{
            String signData = DingTalkSignatureUtil.computeSignature(secret, stringToSign);
            String sign = URLEncoder.encode(signData,"UTF-8");
            return sign;
        } catch (UnsupportedEncodingException e) {
            logger.error(e.getLocalizedMessage(),e.fillInStackTrace());
            throw new RuntimeException("加签失败！请检查签名串");
        }
    }

}